Vue.component('myheader',{
    template: `
        <header class="container">
          <nav class="navbar navbar-default">
            <div class="container-fluid">
              <div class="navbar-header">
                <a class="navbar-brand" href="#">
                  <img alt="Brand" src="images/logo.png">
                </a>
              </div>
              <!--分类开始-->
              <ul class="nav navbar-nav">
                <li><a href="/">首页</a></li>
                <li v-for="c in arr"><a :href="'/index.html?cid='+c.id">{{c.name}}</a></li>
                <li>
                  <form action="">
                    <input type="text" v-model="wd" placeholder="Search">
                    <button type="button" @click="search()"><i class="fa fa-search"></i></button>
                  </form>
                </li>
              </ul>
              <!--分类结束-->
              <ul class="nav navbar-nav navbar-right">
                <!--未登录时显示-->
                <li v-if="user == ''"><a href="/login.html">管理员入口</a></li>
                <!--登录后显示-->
                <li v-if="user != ''"><a href="/admin.html">管理员入口</a></li>
              </ul>
            </div>
          </nav>
        </header>
    `,
    data:function () {
        return {
            arr:[],
            user:"",
            wd:""
        }
    },
    created:function () {
        //此处this代表的是当前的模板组件
        let v = this;
        axios.get("/category/select").then(function (response) {
            //this不是当前的模板组件
            v.arr = response.data;
        })
        //获取当前登录的用户对象
        axios.get("/currentUser").then(function (response) {
            v.user = response.data;
        })
    },
    methods:{
        search:function () {
            /*this代表当前的自定义模板组件，this.wd会自动去上面的data中找到wd变量*/
            location.href = "/index.html?wd="+this.wd;
        }
    }
})

let header_v = new Vue({
    el:"myheader",
})
